<?php

class Model_Users {

    public static function isLogged() {
        return Zend_Auth::getInstance()->hasIdentity();
    }

    public static function logout() {
        Zend_Auth::getInstance()->clearIdentity();
        Zend_Session::destroy();
    }

    public function add($userData) {
        // Save age as initial value
        switch ($userData['age']){
           case 1: $userData['age'] = '11-20';
                break;
           case 2: $userData['age'] = '21-30';
                break;
           case 3: $userData['age'] = '31-40';
                break;
           default: $userData['age'] = '';
                break;
        }
        // @todo: set new email in constract method
        $newUserData = array('email'   => $userData['email'],
                             'name'    => isset($userData['name'])?$userData['name']:'',
                             'password'=> md5($userData['password']),
                             'location_id' => $userData['location'],
                             'sex'     => isset($userData['sex'])?$userData['sex']:'',
                             'age'     => $userData['age'],
                             'about'   => isset($userData['about'])?$userData['about']:'',
        );
        $dbTable = new Model_DbTable_Users();
        $insertResult = $dbTable->insert($newUserData);
        return ($insertResult) ? $insertResult : 0; // if something went wrong, return default value
    }

   public function save($userData) {

    $dbTable = new Model_DbTable_Users();
    $where['id=?'] = $userData['id'];
    $editedUserData = array(
        'name'    => $userData['name'],
        'email'   => $userData['email'],
//      'password'=> $userData['password'] ? md5($userData['password']):'',
        'location_id'=> $userData['location'],
        'role_id' => $userData['privacy'],
//      'image'   => $userData['image'] ? $userData['image'] : '',
        'sex'     => $userData['sex'] ? $userData['sex'] : '',
        'age'     => $userData['age'] ? $userData['age'] : '',
        'about'   => $userData['about'] ? $userData['about'] : '',
    );

    if ($dbTable->update($editedUserData, $where)) {
        return true;
    }
    return false;
   
}

    public function delete($id) {
        $this->_getDbTable()->delete('id = ' . $id);
    }

    /**
     * Geters works OK
     */
    public function getByUserId($id) {
      $manager = Zend_Registry::get('Cache_Manager');
      $dbCache = $manager->getCache('database');
      $key = 'getUserById';
      //if ($dbCache->hasCache($key)) {
         //$userData = $dbCache->getCache($key);
      //} else {
        $model = new Model_DbTable_Users();
        $where['id=?'] = $id;
        $userDataObject = $model->fetchRow($where);
        $userData = array(
            'id'      => $userDataObject->id,
            'name'    => $userDataObject->name,
            'email'   => $userDataObject->email,
            'password'=> '',
            'location'=> $userDataObject->location_id,
            'privacy' => $userDataObject->role_id,
            //'image'   => $userDataObject->image ? $userDataObject->image : '',
            'sex'     => $userDataObject->sex ? $userDataObject->sex : '',
            'age'     => $userDataObject->age ? $userDataObject->age : '',
            'about'   => $userDataObject->about ? $userDataObject->about : '',
        );
       // $dbCache->setCache($key, $userData);
      //}
        //var_dump($userData);die;
        return $userData;
    }
 
    public function getByUsername($username) {
        $model = new Model_DbTable_Users();
        $where['username=?'] = $username;
        $userDataObject = $model->fetchRow($where);
        //user exists
        if(isset($userDataObject)&&(!empty($userDataObject))){
            $userData = array(
                'id'      => $userDataObject->users_id,
                'username'=> $userDataObject->username,
                'email'   => $userDataObject->email,
                'password'=> '',
                'image'   => $userDataObject->image ? $userDataObject->image : '',
                'sex'     => $userDataObject->sex ? $userDataObject->sex : '',
                'age'     => $userDataObject->age ? $userDataObject->age : '',
                'about'   => $userDataObject->about ? $userDataObject->about : '',
            );
        } else {
            $userData = array();
        }
      return $userData;
    }

//   public function getByPlaceAndUserId($userId, $placeId) {
//      $model = new Model_DbTable_Users();
//      $where['id=?'] = $userId;
//      $where['location_id=?'] = $placeId;
//      $userDataObject = $model->fetchRow($where);
//
//      $userData = array(
//         'id'      => $userDataObject->id,
//         'name'    => $userDataObject->name,
//         'email'   => $userDataObject->email,
//         'password'=> '',
//         'location'=> $userDataObject->location_id,
//         'privacy' => $userDataObject->role_id,
//         //'image'   => $userDataObject->image ? $userDataObject->image : '',
//         'sex'     => $userDataObject->sex ? $userDataObject->sex : '',
//         'age'     => $userDataObject->age ? $userDataObject->age : '',
//         'about'   => $userDataObject->about ? $userDataObject->about : '',
//      );
//      return $userData;
//    }

    public function getPublicPlacesOwnerIds() {
        $publicModificator = 1;
        $configObject = new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini", APPLICATION_ENV);
        $configArray = $configObject->toArray();
        $dbOptions = $configArray['resources']['db'];
        $db = Zend_Db::factory($dbOptions['adapter'], $dbOptions['params']);
        $select = $db->select()
                    ->from('users', 'id')
                    ->where('role_id=?', $publicModificator);
        $stmt = $db->query($select);
        $result = $stmt->fetchAll();
        return $result;
    }
}